//
//  StatusCell.swift
//  SinaWeibo
//
//  Created by gaoliang on 15/11/15.
//  Copyright © 2015年 gaoliang. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellMargin: CGFloat = 12
let StatusCellIconWitdth: CGFloat = 35

class StatusCell: UITableViewCell {

  
    // 原创微博底部约束 用来记录底部约束 后序根据是否有配图 来动态修改
    var bottomConstraints: Constraint?
    
    // 数据模型
    var status: Status? {
    
        didSet {
            // 设置原创微博的数据模型
            originalView.status = status
            
            // 移除约束
            self.bottomConstraints?.uninstall()
            
            // 设置转发微博的数据模型
            if let re = status?.retweeted_status{
                // 设置数据源
                retweetedView.retweetedStatus = re
                // 设置显示
                retweetedView.hidden = false
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
            } else {
                // 设置隐藏
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 刷新 UI
    private func setupUI() {
     
        // 添加子控件
        contentView.addSubview(originalView)
       
        contentView.addSubview(retweetedView)
         contentView.addSubview(bottomView)
        
        // 设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
        }
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        //设置约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            // 更改约束
            self.bottomConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
        }
      
       
        // 容器视图的底部约束,是自动计算行高的关键
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(bottomView.snp_bottom)
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
    }
    
    // 懒加载子控件
    private lazy var originalView: StatusOriginalView = StatusOriginalView()
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    private lazy var retweetedView: ReweetedStatusView = ReweetedStatusView()
    
}
